package houserent.service;

import houserent.domain.House;

/**
 * 定义House[],保存House对象
 * 相应HouseService
 */
public class HouseService {
    private House[] houses;//保存house对象
    private int houseNums = 1;//记录当前房屋信息数量
    private int idCount = 1;//id自增长
    public HouseService(int size){
        houses = new House[size];//当创建HouseService对象,指定数组大小
        houses[0] = new House(1,"ahu","177","安徽铜陵",200,"未出租");
    }

    //find,查找房屋信息
    public House find(int findId){
        int index = -1;
        for (int i = 0; i <houseNums ; i++) {
            if (findId == houses[i].getId()){
                return houses[i];
            }
        }
        return null;
    }
    //del方法，删除一个房屋信息
    public boolean del(int delId){
        //找到删除房屋对应的下标
        int index = -1;
        for (int i = 0; i < houseNums; i++) {
            if (delId == houses[i].getId()){
                index = i;
            }
        }
        if (index == -1){//不在数组中
            return false;
        }
        for (int i = index; i <houseNums-1 ; i++) {
            houses[i] = houses[i+1];
        }
        houses[houseNums-1] = null;
        houseNums--;//减少一个
        return true;
    }


    //add方法,添加新的对象
    public boolean add(House newhouse){
        if (houseNums == houses.length){
            System.out.println("不能继续添加，已满！！！");
            return false;
        }
        houses[houseNums++] = newhouse;
        //设计一个id自增长的机制
        newhouse.setId(++idCount);
        return true;
    }

    public House[] list(){
        return houses;
    }
}
